﻿using Microsoft.Data.SqlClient;
using MySqlConnector;
using SqlSugar;
using System.Data;

namespace Inventorycount.API.Write.Extensions
{
    public static class ProduceExtensions
    {
        public static IServiceCollection AddServivces(this IServiceCollection services)
        {
            services.AddScoped<ISqlSugarClient>(s =>
            {
                //Scoped用SqlSugarClient 
                SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.MySql ,
                    ConnectionString = "server=47.99.71.104;uid=guyufan;pwd=Guyufan123!;database=Inventorycount;",
                    IsAutoCloseConnection = true,
                });
                return sqlSugar;
            });
            return services;
        }
        public static IServiceCollection DapperHeaper(this IServiceCollection services, WebApplicationBuilder builder)
        {
            // 正确方式：通过已存在的配置获取连接字符串
            services.AddScoped<IDbConnection>(provider =>
            {
               
                    var connection = new MySqlConnection(builder.Configuration.GetConnectionString("str"));
                    connection.Open();
                    return connection;
               
                 
            });
            return services;
        }
    }
}
